package com.achievo.know.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import com.dayatang.domain.BaseEntity;
import com.dayatang.domain.InstanceFactory;
/**
 * 文件
 * @author hp
 *
 */
@Entity
@Table(name="RAMS_FILE")
public class File extends BaseEntity{
	
	public static final String FAVORITE="FAVORITE";

	/**
	 * 
	 */
	private static final long serialVersionUID = -4933284577438956164L;
	
	//文件名
	@NotNull
	@Column(name="FILE_NAME",nullable=false,length=100)
	private String fileName;
	
	//分类
	@Column(name="CATEGORY",length=50)
	private String category;
	
	@Column(name="CATEGORY_ID")
	private Long categoryId;
	
	//浏览量
	@Column(name="BROWSER_SUM")
	private int browserSum;
	
	//下载量
	@Column(name="DOWN_SUM")
	private int downSum;
	
	//评论量
	@Column(name="FAQ_SUM")
	private int faqSum;
	
	//是否可下载
	@Transient//@Column(name="CAN_DOWN")
	private String canDownStr;
	
	//是否可评议
	@Transient//@Column(name="CAN_FAQ")
	private String canFaqStr;
	
	//是否可下载
	@Column(name="CAN_DOWN")
	private boolean canDown;
	
	//是否可评议
	@Column(name="CAN_FAQ")
	private boolean canFaq;
	
	//备注
	@Column(name="DESCRIPTION",length=2000)
	private String desc;
	
	//文件夹
	@Column(name="FOLDER",length=100)
	private String folder;
	
	//关键字
	@Column(name="KEYWORD",length=200)
	private String keyword;
	
	//发布时间
	@Column(name="PUBLISH_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date publishTime;
	
	//发布人
	@Column(name="PUBLISH_USER",length=25)
	private String publishUser;
	
	//发布时间
	@Column(name="EDIT_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date editTime;
	
	//发布人
	@Column(name="EDIT_USER",length=25)
	private String editUser;
	//上传人
	@NotNull
	@Column(name="UPLOAD_USER",length=25,nullable=false)
	private String uploadUser;
	
	@Column(name="PUBLISH_USER_NAME",length=25)
	private String publishUserName;
	
	//上传时间
	@NotNull
	@Column(name="UPLOAD_TIME",nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date uploadTime;
	
	//不通过原因
	@Column(name="REJECT_REASON",length=200)
	private String rejectReason;
	
	//状态
	@NotNull
	@Column(name="STATUS",length=10,nullable=false)
	private String status;
	
	//附件
	//@JoinColumn(name="FK_FILE_ID")
	//@OneToMany(mappedBy = "file", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
	@Transient
	private List<FileAttachment> attachments;
	
	@Transient
	private Date addFavTime;
	
	@Transient
	private Long tempId;
	
	@Column(name="ATTACH_POSITION")
	private Integer attachPosition;
	
	public File(){
		super();
	}
	
	public File(Long id,String fileName, String category, Date addFavTime) {
		super();
		this.id=id;
		this.fileName = fileName;
		this.category = category;
		this.addFavTime = addFavTime;
	}

	
	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Integer getAttachPosition() {
		return attachPosition;
	}

	public void setAttachPosition(Integer attachPosition) {
		this.attachPosition = attachPosition;
	}

	public boolean isCanFaq() {
		return canFaq;
	}

	public String getPublishUserName() {
		return publishUserName;
	}

	public void setPublishUserName(String publishUserName) {
		this.publishUserName = publishUserName;
	}

	public void setCanFaq(boolean canFaq) {
		this.canFaq = canFaq;
	}

	public boolean isCanDown() {
		return canDown;
	}

	public void setCanDown(boolean canDown) {
		this.canDown = canDown;
	}


	public String getFileName() {
		return fileName;
	}

	
	public Date getEditTime() {
		return editTime;
	}

	public void setEditTime(Date editTime) {
		this.editTime = editTime;
	}

	public String getEditUser() {
		return editUser;
	}

	public void setEditUser(String editUser) {
		this.editUser = editUser;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public int getBrowserSum() {
		return browserSum;
	}

	public void setBrowserSum(int browserSum) {
		this.browserSum = browserSum;
	}

	public int getDownSum() {
		return downSum;
	}

	public void setDownSum(int downSum) {
		this.downSum = downSum;
	}

	
	public Long getTempId() {
		return tempId;
	}

	public void setTempId(Long tempId) {
		this.tempId = tempId;
	}

	public int getFaqSum() {
		return faqSum;
	}

	public void setFaqSum(int faqSum) {
		this.faqSum = faqSum;
	}

	
	public String getCanDownStr() {
		return canDownStr;
	}

	public void setCanDownStr(String canDownStr) {
		this.canDownStr = canDownStr;
	}

	public String getCanFaqStr() {
		return canFaqStr;
	}

	public void setCanFaqStr(String canFaqStr) {
		this.canFaqStr = canFaqStr;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	public String getPublishUser() {
		return publishUser;
	}

	public void setPublishUser(String publishUser) {
		this.publishUser = publishUser;
	}

	public String getUploadUser() {
		return uploadUser;
	}

	public void setUploadUser(String uploadUser) {
		this.uploadUser = uploadUser;
	}

	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	public String getRejectReason() {
		return rejectReason;
	}

	public void setRejectReason(String rejectReason) {
		this.rejectReason = rejectReason;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<FileAttachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<FileAttachment> attachments) {
		this.attachments = attachments;
	}

	
	public Date getAddFavTime() {
		return addFavTime;
	}

	public void setAddFavTime(Date addFavTime) {
		this.addFavTime = addFavTime;
	}
	

	@Override
	public boolean equals(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	private static FileRepository fileRepository;
	
	public static FileRepository fileRepository() {
		if (fileRepository == null) {
			fileRepository = InstanceFactory.getInstance(FileRepository.class);
		}
		return fileRepository;
	}
	
	//新增
	public void add(){
		fileRepository().save(this);
	}
	
	//更新
	public void update(){
		fileRepository().save(this);
	}
	
	//获取
	public File get(Long Id){
		return fileRepository().get(Id);
	}
	
	//删除
	public void del(Long Id){
		fileRepository().remove(get(Id));
	}
	//是否被收藏
	public boolean isAddFavorite(String username){
		return fileRepository().isAddFavorite(this.getId(),username);
	}
	//增加浏览量
	public void addBrowserSum(){
		this.browserSum=this.browserSum+1;
		update();
	}
	//增加下载量
	public void addDownSum(){
		this.downSum=this.downSum+1;
		update();
	}
}
